<?php
class stflickr_model extends stmodel_2_1{

    public function __construct($config) {
        parent::__construct ( $config );
        $this->setTableName($this->prefix.'st_flickr');
        $this->setPrimaryKey('id');
    }

    public function savestFlickrToolbox($photos, $user_id) {
        $dataInsert = array();
        $dataUpdate = array();
        $i=0;
        if(count($photos) > 1){
            foreach ($photos as $photo) {
                $id = $this->getId($photo['id']);
                if($id){
                    $dataUpdate[$i]['id'] = $id;
                    $dataUpdate[$i]['photo_id'] = $photo['id'];
                    $dataUpdate[$i]['user_id'] = $user_id;
                    $dataUpdate[$i]['series'] = $photo['secret'].'|*|'.$photo['server'].'|*|'.$photo['farm'];
                    $dataUpdate[$i]['title'] = $photo['title'];
                    $dataUpdate[$i]['visibility'] = $photo['ispublic'].'|*|'.$photo['isfriend'].'|*|'.$photo['isfamily'];
                    $dataUpdate[$i]['description'] = $photo['description'];

                    $tag_tmp = array();
                    if(count($photo['tags']['tag']) >0){
                        foreach ($photo['tags']['tag'] as $tag) {
                            $tag_tmp[] = $tag['raw'];
                        }
                    }
                    $dataUpdate[$i]['tag'] = implode(',', $tag_tmp);

                    $dataUpdate[$i]['numviews'] = $photo['views'];
                }else{
                    $dataInsert[$i]['id'] = 'NULL';
                    $dataInsert[$i]['photo_id'] = $photo['id'];
                    $dataInsert[$i]['user_id'] = $user_id;
                    $dataInsert[$i]['series'] = $photo['secret'].'|*|'.$photo['server'].'|*|'.$photo['farm'];
                    $dataInsert[$i]['title'] = $photo['title'];
                    $dataInsert[$i]['visibility'] = $photo['ispublic'].'|*|'.$photo['isfriend'].'|*|'.$photo['isfamily'];
                    $dataInsert[$i]['description'] = $photo['description'];

                    $tag_tmp = array();
                    if(count($photo['tags']['tag']) >0){
                        foreach ($photo['tags']['tag'] as $tag) {
                            $tag_tmp[] = $tag['raw'];
                        }
                    }
                    $dataInsert[$i]['tag'] = implode(',', $tag_tmp);

                    $dataInsert[$i]['numviews'] = $photo['views'];
                }
                $i++;
            }
        }

        $this->inserRecords($dataInsert) ;
        $this->updateRecords($dataUpdate);
    }

    public function inserRecords($data = '', $where = ''){
        if(count($data)>0){
            $query = "INSERT INTO $this->_tableName
                    ( id, photo_id, user_id, series, visibility, title, description, tag, numviews )
                    VALUES
                    ";
            $query1 = array();
            foreach ($data as $value) {
                $query1[] = $this->prepare("(%d, %s, %d, %s, %s, %s, %s, %s, %d)",
                        $value['id'], $value['photo_id'], $value['user_id'], $value['series'], $value['visibility']
                        , $value['title'], $value['description'], $value['tag'], $value['numviews']
                        );
            }
            $query1 = implode(', ', $query1);
            $query .= ' '.$query1;
//            echo $query;
            return $this->query( $query );
        }else{
            return false;
        }
    }

    public function updateRecords($data = '', $where = ''){
        if(count($data)>0){
            foreach ($data as $value) {
                $where = array($this->_primaryKey => $value[$this->_primaryKey]);
                $this->update($this->_tableName, $value, $where);
            }
        }
        return false;
    }

    public function getId($photoId){
        $query = "SELECT id
        FROM $this->_tableName
        WHERE photo_id like ('$photoId')
        ";
        $result = $this->get_var($query);
        return $result;
    }

    public function getRecords($args = '') {
        $default_args = array(
            'fields' => '*',
            'and_where' => '',
            'or_where' => '',
            'where' => '',
            'order' => '',
            'limit' => '',
            'group_by' => '',
            'custom_query' => '',
        );
        if ($args == '')
            $args = $default_args;

        $fields = $args['fields'] ? $args['fields'] : '*';
        $and_where = $args['and_where'] ? $args['and_where'] : '';
        $or_where = $args['or_where'] ? $args['or_where'] : '';
        $custom_where = $args['where'] ? $args['where'] : '';
        $order = $args['order'] ? $args['order'] : '';
        $limit = $args['limit'] ? $args['limit'] : '';
        $group_by = $args['group_by'] ? $args['group_by'] : '';
        $custom_query = $args['custom_query'] ? $args['custom_query'] : '';
        if (!empty($custom_query)) {
            $query = $custom_query;
        } else {
            if ($fields != '*')
                $fields = implode(', ', $fields);
            //
            $where = ' WHERE ';

            if ($and_where != '')
                $where .= implode('', $and_where);
            if ($or_where != '')
                $where .= implode('', $or_where);
            if ($custom_where != '')
                $where .= $custom_where;
            if ($where == ' WHERE ')
                $where = '';
            //
            if ($order != '')
                $order = ' ORDER BY ' . implode(' ', $order);
            //
            if ($limit != '')
                $limit = ' LIMIT ' . implode(', ', $limit);
            if ($group_by != '')
                $group_by = ' GROUP BY ' . $group_by;
            //

            $table_set = $this->prefix.'st_flickr_set';
            $table_set_photo = $this->prefix.'st_flickr_set_photo';
            $query = "SELECT $fields
            FROM $this->_tableName photo
            LEFT JOIN $table_set_photo set_photo on set_photo.photo_id = photo.photo_id
            LEFT JOIN $table_set settable on settable.set_id = set_photo.set_id
            $where
            $group_by
            $order
            $limit
            ";
        }
//        echo $query.'<br>';
        return $this->get_results($query);
    }

    public function countRecords($args = ''){

        $result = $this->getRecords($args);
        return count($result);

    }
}